package com.future.lock.common;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.future.baselib.entity.MessageEvent;
import com.future.baselib.utils.EventBusUtil;
import com.future.baselib.utils.UserInfoSharePreference;
import com.future.lock.common.activity.MainActivity;
import com.future.lock.common.http.HttpUtils;
import com.future.lock.home.entity.bean.Lock;
import com.future.lock.home.entity.response.GatewayListResponse;
import com.future.lock.home.entity.response.LockListResponse;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by Administrator on 2017/12/26 0026.
 */

public class RefreshDataUtils {

    public static void refreshGatewayList(Context context,RefreshCallback callback){
        HttpUtils.getHttpApi().getGatewayList(MyApp.getUserToken()).enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                GatewayListResponse gatewayListResponse = new GatewayListResponse();
                gatewayListResponse.parse(response.body());
                if (gatewayListResponse.success == 1){
                    Constants.gatewayList = gatewayListResponse.gateWays;
                    callback.refreshResult(true);
                }else{
                    if (gatewayListResponse.code == 112) {
                        logout(context);
                        Toast.makeText(context, "登录失效，请重新登录", Toast.LENGTH_SHORT).show();
                        callback.refreshResult(false);
                    }else {
                        Toast.makeText(context, "网关列表获取失败", Toast.LENGTH_SHORT).show();
                        callback.refreshResult(false);
                    }
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show();
                callback.refreshResult(false);
            }
        });
    }

    public static void refreshLockList(Context context,RefreshCallback callback){
        HttpUtils.getHttpApi().getLockList(MyApp.getUserToken()).enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                LockListResponse lockListResponse = new LockListResponse();
                lockListResponse.parse(response.body());
                if (lockListResponse.success == 1) {
                    Constants.lockList = lockListResponse.locks;
                    callback.refreshResult(true);
                }else{
                    if (lockListResponse.code == 112) {
                        logout(context);
                        Toast.makeText(context, "登录失效，请重新登录", Toast.LENGTH_SHORT).show();
                        callback.refreshResult(false);
                    }else {
                        Toast.makeText(context, "智能锁列表获取失败", Toast.LENGTH_SHORT).show();
                        callback.refreshResult(false);
                    }
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show();
                callback.refreshResult(false);
            }
        });
    }

    public static void logout(Context context){
        UserInfoSharePreference.getInstance(context).setUserToken("");
        MyApp.setUserInfo(null);
        MyApp.setUserToken("");
        EventBusUtil.sendEvent(new MessageEvent(Constants.REFRESH_LOCK_DATA));
        Intent intent = new Intent(context,MainActivity.class);
        Bundle bundle = new Bundle();
        bundle.putBoolean("logout",true);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    public interface RefreshCallback{
        void refreshResult(boolean refresh);
    }
}
